//
//  ReserverInfoCell.swift
//  koolcar
//
//  Created by xiantian on 2023/11/29.
//

import UIKit
import RxSwift
protocol ReserverInfoCellDelegate: NSObjectProtocol {
    func didSelectGender()
}
class ReserverInfoCell: UITableViewCell {
    var delegate: ReserverInfoCellDelegate?
    var disBag = DisposeBag()
    var genderTF: ReserverInputView!
    var lastNameTF: ReserverInputView!
    var firstNameTF: ReserverInputView!
    var phoneTF: ReserverInputView!
    var emailTF: ReserverInputView!
    var infoObject: ReserverInfo?
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        genderTF = ReserverInputView()
        genderTF.titleTxt = MMLocalizedString("profile_salutation", comment: "称谓")
        genderTF.inputTFView.placeholder = MMLocalizedString("please_select", comment: "请选择")
        genderTF.inputTFView.textField.textAlignment = .left
        self.contentView.addSubview(genderTF)
        
        let arrowImgBtn = UIButton.init(frame: CGRect.init(x: 0, y: 0, width: 44, height: 54))
        arrowImgBtn.setup_image(image: UIImage.init(named: "icon_drop_full"))
        genderTF.inputTFView.rightView = arrowImgBtn
        genderTF.inputBtn.isHidden = false
        genderTF.inputBtn.addTarget(self, action: #selector(inputBtnAction), for: .touchUpInside)
        
        lastNameTF = ReserverInputView()
        lastNameTF.titleTxt = MMLocalizedString("profile_family", comment: "姓氏")
        self.contentView.addSubview(lastNameTF)
        
        firstNameTF = ReserverInputView()
        firstNameTF.titleTxt = MMLocalizedString("profile_name", comment: "名字")
        self.contentView.addSubview(firstNameTF)
        
        phoneTF = ReserverInputView()
        phoneTF.titleTxt = MMLocalizedString("profile_phone", comment: "手提电话")
        self.contentView.addSubview(phoneTF)
        
        emailTF = ReserverInputView()
        emailTF.titleTxt = MMLocalizedString("profile_email", comment: "电邮地址")
        self.contentView.addSubview(emailTF)
        
        lastNameTF.inputTFView.textField.rx.text.orEmpty.subscribe { txt in
            self.infoObject?.lastName = txt.element ?? ""
        }.disposed(by: self.disBag)
        
        firstNameTF.inputTFView.textField.rx.text.orEmpty.subscribe { txt in
            self.infoObject?.firstName = txt.element ?? ""
        }.disposed(by: self.disBag)
        
        phoneTF.inputTFView.textField.rx.text.orEmpty.subscribe { txt in
            self.infoObject?.phone = txt.element ?? ""
        }.disposed(by: self.disBag)
        
        emailTF.inputTFView.textField.rx.text.orEmpty.subscribe { txt in
            self.infoObject?.email = txt.element ?? ""
        }.disposed(by: self.disBag)
        
        self.addSnpLayout()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func addSnpLayout(){
        genderTF.snp.makeConstraints { make in
            make.left.equalTo(14)
            make.top.equalTo(12)
            make.width.equalTo(80)
            make.height.greaterThanOrEqualTo(60)
        }
        
        lastNameTF.snp.makeConstraints { make in
            make.left.equalTo(genderTF.snp.right).offset(10)
            make.top.equalTo(12)
            make.height.greaterThanOrEqualTo(60)
        }
        
        firstNameTF.snp.makeConstraints { make in
            make.left.equalTo(lastNameTF.snp.right).offset(10)
            make.top.equalTo(12)
            make.right.equalTo(-14)
            make.width.equalTo(lastNameTF.snp.width)
            make.height.greaterThanOrEqualTo(60)
        }
        
        phoneTF.snp.makeConstraints { make in
            make.left.equalTo(14)
            make.top.equalTo(lastNameTF.snp.bottom).offset(10)
            make.right.equalTo(-14)
        }
        
        emailTF.snp.makeConstraints { make in
            make.left.equalTo(14)
            make.top.equalTo(phoneTF.snp.bottom).offset(10)
            make.right.equalTo(-14)
            make.height.greaterThanOrEqualTo(60)
            make.bottom.equalTo(-10)
        }
    }
    /**
     MMLocalizedString("add_gender_Mr", comment: "先生"),
     MMLocalizedString("add_gender_Mrs", comment: "女士"),
     MMLocalizedString("add_gender_Miss", comment: "小姐")
     */
    func setReserverInfoCell(data: ReserverInfo) {
        self.infoObject = data
        switch data.gender {
            case 0:
                //女士
                self.genderTF.inputTFView.textField.text = MMLocalizedString("add_gender_Mrs", comment: "女士")
            case 1:
                //选生
                self.genderTF.inputTFView.textField.text = MMLocalizedString("add_gender_Mr", comment: "先生")
            case 2:
                //小姐
                self.genderTF.inputTFView.textField.text = MMLocalizedString("add_gender_Miss", comment: "小姐")
            default:
                ()

        }
        self.lastNameTF.inputTFView.textField.text = data.lastName
        self.firstNameTF.inputTFView.textField.text = data.firstName
        self.phoneTF.inputTFView.textField.text = data.phone
        self.emailTF.inputTFView.textField.text = data.email
    }
    
    ///称谓
    @objc func inputBtnAction(){
        self.delegate?.didSelectGender()
    }
}
